package com.ruoyi.web.controller.system.api.cc;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.framework.interceptor.Authentication;
import com.ruoyi.system.api.ResponseWithData;
import com.ruoyi.system.api.domain.ReportInfo;
import com.ruoyi.system.api.domain.ServiceNotice;
import com.ruoyi.system.api.domain.StErrorReport;
import com.ruoyi.system.api.service.IReportInfoService;
import com.ruoyi.system.api.service.IServiceNoticeService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报告主要信息维护
 *
 * @author yzg
 * @ClassName ReportInfoController.java
 * @date 2020/3/9 8:52 上午
 */
@Api(tags = "报告主要信息维护")
@RestController
@RequestMapping("/api/cc/report/info")
public class ReportInfoController extends BaseController {

    @Autowired
    private IReportInfoService reportInfoService;

    private static final Logger log = LoggerFactory.getLogger(ReportInfoController.class);

    /**
     * 删除报告
     *
     * @param id
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author yzg
     * @date 2020/3/6 9:53 上午
     */
    @Log(title = "删除报告", businessType = BusinessType.DELETE)
    @GetMapping("/deleteReport")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "删除报告", httpMethod = "GET", notes = "删除报告")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "报告主键id（st_report_info表主键）", name = "id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "工程师主键ID", name = "enginnerId", required = true, defaultValue = "10009", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(value = "服务编号", name = "serviceNo", required = false, defaultValue = "10009", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "辅助ID", name = "helpId", required = false, defaultValue = "10009", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "报告类型", name = "type", required = false, defaultValue = "10009", dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> deleteReport(@RequestParam("id") Long id, @RequestParam(value = "enginnerId") Long enginnerId,
                                            @RequestParam(value = "serviceNo", required = false) String serviceNo,
                                            @RequestParam(value = "helpId", required = false) String helpId,
                                            @RequestParam(value = "type", required = false) String type) {
        try {
            return reportInfoService.deleteReport(id, enginnerId, serviceNo, helpId, type);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 查询报告列表
     *
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author sjy
     * @date 2020年3月9日19:32:42
     */
    @GetMapping("/getReportList")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "查询报告列表", httpMethod = "GET", notes = "查询报告列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "服务号", name = "serviceNo", required = false, defaultValue = "10009", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "工程师主键ID", name = "EnginnerId", required = false, defaultValue = "10009", dataType = "Long", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> getReportList(@RequestParam(value = "serviceNo", required = false) String serviceNo,
                                             @RequestParam(value = "EnginnerId", required = false) Long EnginnerId) {
        try {
            ReportInfo reportInfo = new ReportInfo();
            reportInfo.setServiceNo(serviceNo);
            reportInfo.setEnginnerId(EnginnerId);
            List<ReportInfo> list = reportInfoService.getReportList(reportInfo);
            return new ResponseWithData<>().success(list).msg("查询成功").code(200);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }


    /**
     * 查询报告列表（关联主表）
     *
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author sjy
     * @date 2020年3月9日19:32:42
     */
    @GetMapping("/getReportListByEnginnerId")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "查询报告列表（关联主表）", httpMethod = "GET", notes = "查询报告列表（关联主表）")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "工程师主键ID", name = "EnginnerId", required = true, defaultValue = "10009", dataType = "Long", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> getReportListByEnginnerId(@RequestParam(value = "EnginnerId", required = true) Long EnginnerId) {
        try {
            ServiceNotice serviceNotice = new ServiceNotice();
            serviceNotice.setEnginnerId(EnginnerId);
            List<ServiceNotice> list = reportInfoService.getReportListByEnginnerId(serviceNotice);
            return new ResponseWithData<>().success(list).msg("查询成功").code(200);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }


    /**
     * 客户签名列表页
     *
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author sjy
     * @date 2020年3月21日13:10:52
     */
    @GetMapping("/getCustomerSignList")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "客户签名列表页", httpMethod = "GET", notes = "客户签名列表页")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "服务号", name = "serviceNo", required = true, defaultValue = "10009", dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> getCustomerSignList(@RequestParam(value = "serviceNo", required = true) String serviceNo) {
        try {
            ReportInfo reportInfo = new ReportInfo();
            reportInfo.setServiceNo(serviceNo);
            List<ReportInfo> list = reportInfoService.getCustomerSignList(reportInfo);
            return new ResponseWithData<>().success(list).msg("查询成功").code(200);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 客户签名列表页
     *
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author sjy
     * @date 2020年3月21日13:10:52
     */
    @GetMapping("/getAllCustomerSignList")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "客户签名列表页", httpMethod = "GET", notes = "客户签名列表页")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "服务号", name = "serviceNo", required = true, defaultValue = "10009", dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> getAllCustomerSignList(@RequestParam(value = "serviceNo", required = true) String serviceNo) {
        try {
            ReportInfo reportInfo = new ReportInfo();
            reportInfo.setServiceNo(serviceNo);
            List<ReportInfo> list = reportInfoService.getAllCustomerSignList(reportInfo);
            return new ResponseWithData<>().success(list).msg("查询成功").code(200);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 验证该服务单是否有未提交报告
     *
     * @param serviceNo
     * @param enginnerId
     * @param syncFlag
     * @methodName: checkReport
     * @description: TODO
     * @return: com.ruoyi.system.api.ResponseWithData<?>
     * @author: shihx
     * @date: 2020-04-22 9:58
     **/
    @GetMapping("/checkReport")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "验证该服务单是否有未提交报告", httpMethod = "GET", notes = "验证该服务单是否有未提交报告")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "服务号", name = "serviceNo", required = false, defaultValue = "10009", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "工程师主键ID", name = "enginnerId", required = false, defaultValue = "10009", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(value = "是否提交CC端 0未提交 1已提交", name = "syncFlag", required = false, defaultValue = "10009", dataType = "Long", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> checkReport(@RequestParam(value = "serviceNo", required = false) String serviceNo,
                                           @RequestParam(value = "enginnerId", required = false) Long enginnerId,
                                           @RequestParam(value = "syncFlag", required = false) String syncFlag) {
        try {
            ReportInfo reportInfo = new ReportInfo();
            reportInfo.setServiceNo(serviceNo);
//            reportInfo.setEnginnerId(enginnerId);
            reportInfo.setSyncFlag(syncFlag);
            List<ReportInfo> list = reportInfoService.selectStReportInfoList(reportInfo);
            return new ResponseWithData<>().success(list).msg("关闭成功").code(200);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 验证该服务单是否有已提交报告
     *
     * @param serviceNo
     * @param planId
     * @methodName: checkReportYtj
     * @description: TODO
     * @return: com.ruoyi.system.api.ResponseWithData<?>
     * @author: wlf
     * @date: 2020-05-26 14:15
     **/
    @GetMapping("/checkReportYtj")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "验证该服务单是否有已提交报告", httpMethod = "GET", notes = "验证该服务单是否有已提交报告")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "服务号", name = "serviceNo", required = true, defaultValue = "10009", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "工作计划id", name = "planId", required = false, defaultValue = "10009", dataType = "Long", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> checkReportYtj(@RequestParam(value = "serviceNo", required = true) String serviceNo,
                                              @RequestParam(value = "planId", required = false) Long planId) {
        try {
            ReportInfo reportInfo = new ReportInfo();
            reportInfo.setServiceNo(serviceNo);
            reportInfo.setPlanId(planId);
            reportInfo.setSyncFlag("1");
            List<ReportInfo> list = reportInfoService.selectStReportInfoList(reportInfo);
            return new ResponseWithData<>().success(list).msg("关闭成功").code(200);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }


    /**
     * 验证该服务单是否有已提交并且服务状态为已完成的服务报告
     *
     * @param serviceNo
     * @param
     * @methodName: checkReportResult
     * @description: TODO
     * @return: com.ruoyi.system.api.ResponseWithData<?>
     * @author: wlf
     * @date: 2020-05-26 14:15
     **/
    @GetMapping("/checkReportResult")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "验证该服务单是否有已提交报告", httpMethod = "GET", notes = "验证该服务单是否有已提交报告")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "服务号", name = "serviceNo", required = true, defaultValue = "10009", dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> checkReportYtj(@RequestParam(value = "serviceNo", required = true) String serviceNo) {
        try {
            ReportInfo reportInfo = new ReportInfo();
            reportInfo.setServiceNo(serviceNo);
            reportInfo.setSyncFlag("1");
            reportInfo.setServiceResult("1");
            List<ReportInfo> list = reportInfoService.selectStReportInfoList(reportInfo);
            return new ResponseWithData<>().success(list).msg("查询成功").code(200);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 查询最新一次报告的工作开始和工作结束
     *
     * @param serviceNo
     * @param enginnerId
     * @methodName: selectReportStartEndTime
     * @description: TODO
     * @return: com.ruoyi.system.api.ResponseWithData<?>
     * @author: wlf
     * @date: 2020-05-16 14:56
     **/
    @GetMapping("/selectReportStartEndTime")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "查询最新一次报告的工作开始和工作结束时间", httpMethod = "GET", notes = "查询最新一次报告的工作开始和工作结束时间")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "服务号", name = "serviceNo", required = true, defaultValue = "10009", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "工程师主键ID", name = "enginnerId", required = true, defaultValue = "10009", dataType = "Long", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> selectReportStartEndTime(
            @RequestParam(value = "serviceNo", required = true) String serviceNo,
            @RequestParam(value = "enginnerId", required = true) Long enginnerId) {
        try {
            Map<String, Object> result = reportInfoService.selectReportStartEndTime(serviceNo, enginnerId);
            return new ResponseWithData<>().success(result).msg("查询成功").code(200);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    @GetMapping("/checkPhoneResolutionReportCompleted")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "验证是否有已提交且已完成的电话解决报告", httpMethod = "GET", notes = "验证是否有已提交且已完成的电话解决报告")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "服务号", name = "serviceNo", required = true, defaultValue = "10009", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "工程师主键ID", name = "enginnerId", required = true, defaultValue = "10009", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(value = "计划ID", name = "planId", required = true, defaultValue = "10009", dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> checkPhoneResolutionReportCompleted(
            @RequestParam(value = "serviceNo", required = true) String serviceNo,
            @RequestParam(value = "enginnerId", required = true) Long enginnerId,
            @RequestParam(value = "planId", required = true) String planId
    ) {
        try {
            return reportInfoService.checkPhoneResolutionReportCompleted(serviceNo, enginnerId, planId);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    @GetMapping("/getServiceStatusByServiceNo")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "查询服务状态", httpMethod = "GET", notes = "查询服务状态")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "服务号", name = "serviceNo", required = true, defaultValue = "10009", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "工程师ID", name = "fseId", required = true, defaultValue = "10009", dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> getServiceStatusByServiceNo(
            @RequestParam(value = "serviceNo", required = true) String serviceNo,
            @RequestParam(value = "fseId", required = true) String fseId
    ) {
        try {
            return reportInfoService.getServiceStatusByServiceNo(serviceNo, fseId);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }


    @GetMapping("/getServiceSignPicHandle")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "获取服务通知签名图片", httpMethod = "GET", notes = "获取服务通知签名图片")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "服务号", name = "serviceNo", required = true, defaultValue = "10009", dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> getServiceSignPicHandle(String serviceNo) {
        try {
            return reportInfoService.getServiceSignPicHandle(serviceNo);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }




}
